//
//  LoginViewController.m
//  EMEIJU_IOS
//
//  Created by Caesar on 16/9/14.
//  Copyright (c) 2016年 Caesar. All rights reserved.
//

#import "LoginViewController.h"
#import "MBProgressHUD.h"
#import "AFHTTPSessionManager.h"

@implementation LoginViewController{
    UITabBarController  *mainTabBar;
    UITextField *nameField;
    UITextField *passField;
    NSUserDefaults *userDefaults;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    userDefaults=[[NSUserDefaults alloc] init];
    mainTabBar = (UITabBarController *)[self.storyboard instantiateViewControllerWithIdentifier:@"mainTabBar"];
    nameField=(UITextField *)[self.view viewWithTag:1001];
    passField=(UITextField *)[self.view viewWithTag:1002];
    UIButton *loginBtn=(UIButton *)[self.view viewWithTag:1003];
    [loginBtn addTarget:self action:@selector(postLogin:) forControlEvents:UIControlEventTouchUpInside];
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]
                                                    initWithTarget:self
                                                    action:@selector(resignKeyBoard:)];
    [self.view addGestureRecognizer:tapGestureRecognizer];
}

- (void)resignKeyBoard:(UITapGestureRecognizer *)recognizer {
    [nameField resignFirstResponder];
    [passField resignFirstResponder];
}

- (void)postLogin:(UIButton *)button
{
    if (nameField.text&&nameField.text.length>0&&passField.text&&passField.text.length>0) {
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        NSDictionary *param=@{@"user_name":nameField.text,@"password":passField.text};
        [[AFHTTPSessionManager manager] POST:@"http://www.fdcanet.com/index.php?app=member&act=login&appid=1&mobile=1" parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (responseObject) {
                NSDictionary *dataDic=responseObject[@"data"];
                NSString *agent_id=dataDic[@"agent_id"];
                NSString *redirect_url=dataDic[@"redirect_url"];
                NSString *login_status=dataDic[@"login_status"];
                if([login_status isEqualToString:@"1"]){
                    //获取cookie
                    NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
                    [userDefaults setObject:dataDic forKey:@"dataDic"];
                    [userDefaults setObject:cookies forKey:@"cookiesArray"];
                    [self.navigationController presentViewController:mainTabBar animated:YES completion:^{
                    }];}else{
                        UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"提示" message:@"对不起，您输入的用户名或密码不正确，请您仔细核对，重新登录。" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                        [alert show];
                    }
            }else{
                UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"提示" message:@"对不起，您输入的用户名或密码不正确，请您仔细核对，重新登录。" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"error:%@",error);
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
        
        }else{
            UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"提示" message:@"请填写完整信息" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
    
    
}



@end
